package com.wanyang.util;

import org.springframework.beans.BeanUtils;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;


/**
 * Description:
 */
public abstract class AssUtils {

    /**
     * 将Exception 的 stack 跟踪信息 转换成Str
     *
     * @param straceElements
     * @return
     */
    public static String parse(StackTraceElement[] straceElements) {
        StringBuffer buff = new StringBuffer();
        int len = straceElements.length;
        for (int i = 0; i < len; i++) {
            buff.append(straceElements[i].toString());
            if (i < len) buff.append(System.getProperty(
                    "line.separator",
                    "/r/n"));
        }
        return buff.toString();
    }


    /**
     * 将Exception 的 stack 跟踪信息 转换成Str
     *
     * @param e
     * @return
     */

    public static String parse(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }


    /**
     * 获取父路径
     *
     * @param ftpFilePath
     * @return
     */
    public static String fetchParentPath(String ftpFilePath) {
        return new File(ftpFilePath).getParent();
    }


    /**
     * @param source
     * @param charset
     * @return
     */
    public static String convertEncoding(String source, String charset) {
        try {
            source = new String(source.getBytes(), charset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return source;
    }

    /**
     * 取得session中的当前用户信息
     *
     * @return
     */
    public static Object getCurrentUser() {
        return null;
    }

    /**
     * 取得session中的当前用户ID
     *
     * @return
     */
    public static String getCurrentUserId() {
        return null;
    }

    /**
     * 是否处于登录
     *
     * @return
     */
    public static Boolean isLogin() {
        return false;
    }


    /**
     * 复制属性
     *
     * @param source
     * @param target
     * @param ignoreProperties
     * @return
     */
    public static <T> void copyProperties(T source, T target,
                                          String... ignoreProperties) {
        BeanUtils.copyProperties(source, target, ignoreProperties);
    }


}
